package com.benyi.util;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JResult {
	protected Logger log = LoggerFactory.getLogger(JResult.class);
	
	private List<String[]> rs;
	
	public JResult(List<String[]> rs){
		this.rs=rs;
		if(rs.size()>0){
			String []cs=rs.get(0);
			for(int i=0;i<cs.length;i++){
				cs[i]=cs[i].toLowerCase();
			}
		}
	}
	
	public JResult(String []colName){
		rs=new ArrayList<String[]>();
		String []cs=new String[colName.length];
		for(int i=0;i<cs.length;i++){
			cs[i]=colName[i].toLowerCase();
		}
		rs.add(cs);
	}
	
	public int addRow(String []row){
		rs.add(row);
		int r=getRows();
		return r;
	}
	
	public void delRowIndex(int index){
		if(getRows()>index){
			rs.remove(index+1);
		}
	}
	
	public void delRows(int from){
		int index=getRows()+1; 
		while(index>from+1){
			index--;
			rs.remove(index);
		}
	}
	
	/**
	 * 取得在 List 中的计录行数
	 * 
	 * @param ls
	 * @return
	 */
	public int getRows() {
		if (rs == null){
			return -1;
		}
		if (rs.size() < 0){
			return 0;
		}
		return rs.size() - 1;
	}

	/**
	 * 取得在 List 中的计录列数
	 * 
	 * @param ls
	 * @return
	 */
	public int getCols() {
		if (rs == null){
			return -1;
		}
		if (rs.size() < 1){
			return -1;
		}
		String[] s = (String[]) rs.get(0);
		return s.length;
	}

	/**
	 * 取得在 List 中的指定行列的字段值
	 * 
	 * @param ls
	 * @param row  - 行号,从0开始
	 * @param ls   - 列号,从0开始
	 * @return
	 */
	public String get (int row, int col) {
		if (row<0 || col<0 || row >= getRows() || col >= getCols()) {
			return null;
		}
		String[] s = (String[])rs.get(row + 1);
		return s[col];
	}
	
	public String[] getRow(int row){
		if (row >= getRows() || row<0) {
			return null;
		}
		String[] s = (String[])rs.get(row + 1);
		return s;
	}

	/**
	 * 取得在 List 中的指定行列名的字段值
	 * 
	 * @param ls
	 * @param row  - 行号,从0开始
	 * @param ls   - 列号,从0开始
	 * @return
	 */
	public String get( int row, String fieldName) {
		if (fieldName == null){
			return null;
		}
		if(row>=getRows()){
			return null;
		}
		String[] c = getColumnNames();
		String[] v = (String[]) rs.get(row + 1);
		for (int i = 0; i < c.length; i++) {
			if (fieldName.equalsIgnoreCase(c[i])) {
				return v[i]; 
			}
		}
		return null;
	}
	
	public int getInt(int row, int col){
		try{
			return Integer.parseInt(get(row,col));
		}catch(Exception e){
			log.info("Can't convert to int!", e);
		}
		return 0;
	}
	
	public int getInt(int row, String fieldName){
		try{
			if(get(row,fieldName)==null){
				return 0;
			}
			return Integer.parseInt(get(row,fieldName));
		}catch(Exception e){
			log.info("Can't convert to int!", e);
		}
		return 0;
	}
	
	public long getLong(int row, int col){
		try{
			return Long.parseLong(get(row,col));
		}catch(Exception e){
			log.info("Can't convert to long!", e);
		}
		return 0;
	}
	
	public long getLong(int row, String fieldName){
		try{
			return Long.parseLong(get(row,fieldName));
		}catch(Exception e){
			log.info("Can't convert to long!", e);
		}
		return 0;
	}
	
	public int getColumnIndex(String fieldName){
		if (fieldName == null)
			return -1;
		String[] c = getColumnNames(); 
		for (int i = 0; i < c.length; i++) {
			if (fieldName.equalsIgnoreCase(c[i])) {
				return i;
			}
		}
		return -1;
	}
	
	public String getColumnName(int index){ 
		String[] c = getColumnNames(); 
		if(c==null) return null; 
		if(index<0 || index>=c.length){
			return null;
		}
		return c[index];
	}

	/**
	 * 取得 List 中的列名
	 * 
	 * @param ls
	 * @return
	 */
	public String[] getColumnNames() {
		if (rs == null || rs.size()==0){
			return null;
		} 
		return rs.get(0);
	}
	
	public String toString(){
		StringBuilder sb=new StringBuilder();
		for(int i=0;i<rs.size();i++){
			if(i>0){
				sb.append("\r\n");
			}
			sb.append(toString(rs.get(i)));			
		}
		return sb.toString();
	}
	
	public static String toString(String []vals){		
		StringBuilder sb=new StringBuilder();
		if(vals==null){
			return sb.toString();
		}
		for(int i=0;i<vals.length;i++){
			if(vals[i]==null){
				sb.append("null");
			}else{
				if(vals[i].indexOf(",")>=0){
					sb.append(vals[i].replace(",","&d"));
				}else{
					sb.append(vals[i]);
				} 
			}
			if(i<vals.length-1){
				sb.append(",");
			}
		}
		return sb.toString();
	}
	
	public static String[] toArray(String line){ 
		if(line==null){
			return null;
		}
		line=line.trim();
		if(line.length()==0){
			return null;
		}
		
		String []cs=line.split(",");
		for(int i=0;i<cs.length;i++){
			if(cs[i].equals("null")){
				cs[i]=null;
			}else{
				if(cs[i].indexOf("&d")>=0){
					cs[i]=cs[i].replace("&d", ",");
				}
			}
		}
		return cs;
	}
	
	
	public static JResult toResult(List<HashMap<String,Object>> rs) {
		if(rs==null) {
			return null;
		} 
		List<String[]> list =new ArrayList<String[]>();
		String[] ts = makeKeyArray(rs);
		list.add(ts);
		HashMap<String,Object> m;
		for(int i=0;i<rs.size();i++) {
			m = rs.get(i);
			String[] one = makeValueArray(m,ts);
			list.add(one);
		}
		return new JResult(list);
	}
	
	private static String[] makeValueArray(HashMap<String,Object> m ,String[] ts) {
		String[] one = new String[ts.length];
		for(int i=0;i<ts.length;i++) {
			one[i] = String.valueOf(m.get(ts[i]));
		}
		return one;
	}
	
	private static String[] makeKeyArray(List<HashMap<String,Object>> rs) {
		ArrayList<String> ts=new ArrayList<String>();
		HashMap<String,Object> m;
		for(int i=0;i<rs.size();i++) {
			m = rs.get(i);
			Set<String> ks = m.keySet(); 
			Iterator<String> e = ks.iterator();
			while(e.hasNext()) {
				String one = e.next(); 
				boolean find  = false;
				for(int j=0;j<ts.size();j++) {
					if(one.equalsIgnoreCase(ts.get(j))) {
						find = true;
						break;
					}
				}
				if(find==false) {
					ts.add(one);
				}
			}
		}
		String []ls = new String[ts.size()];
		for(int i=0;i<ls.length;i++) {
			ls[i]=ts.get(i);
		}
		return ls;
	}
	
	
	public static void main(String []args){
		List<String[]> l=new ArrayList<String[]>();
		l.add(new String[3]);
		l.add(new String[]{"10","2","3"});
		l.add(new String[]{"20","20","30"});
		l.add(new String[]{"30","20","30"});
	//	l.add(new String[]{"40","20","30"});
		JResult r=new JResult(l);
		System.out.println(r.getRows()+","+r.getCols()+":"+r.get(0, 0));
		r.delRowIndex(2);
		System.out.println(r.getRows()+","+r.getCols()+":"+r.get(0, 0));
		r.delRowIndex(0);
		System.out.println(r.getRows()+","+r.getCols()+":"+r.get(0, 0));
		r.delRowIndex(0);
		System.out.println(r.getRows()+","+r.getCols()+":"+r.get(0, 0));
		r.delRowIndex(0);
		System.out.println(r.getRows()+","+r.getCols()+":"+r.get(0, 0));
	}
	
}
